//
//  BaseRuleVC.m
//  WisdomStudy
//
//  Created by 葛 军 on 2018/4/17.
//  Copyright © 2018年 周剑. All rights reserved.
//

#import "BaseRuleVC.h"
#import "Masonry.h"
#import "BarButtonView.h"
#import "WSHttpManager.h"
@interface BaseRuleVC ()

@end

@implementation BaseRuleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self changeNaviColor:[UIColor colorWithRed:11/255.f green:159/255.f blue:1 alpha:1]];
    [self addLeftBtn];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    UIWebView *webView = [UIWebView new];
    webView.opaque = NO;
    webView.backgroundColor = UIColorFromRGB(0xffffff);
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom).mas_offset(0);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    
    [WSHttpManager getAppRuleInfoInCode:self.code complete:^(DCHttpResult result, NSString *errorMessage, id responseData) {
        if (result) {
            NSString *content = [[responseData objectForKey:@"data"] objectForKey:@"content"];
            [webView loadHTMLString:content baseURL:nil];
            self.title = [[responseData objectForKey:@"data"] objectForKey:@"title"];
        }
    }];
    // Do any additional setup after loading the view.
}

-(void)leftBtnAction{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)changeNaviColor:(UIColor *)color{
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //self.navigationController.navigationBar.barStyle=UIStatusBarStyleLightContent;
    //self.navigationController.navigationBar.barTintColor=color;
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:color]
                                                 forBarPosition:UIBarPositionAny
                                                     barMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
-(void)addLeftBtn{
    UIButton * btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"ic_back_white"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 50, 40);
    [btn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 25)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 0)];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    //     UIBarButtonItem * itemleftback = [[UIBarButtonItem alloc]initWithCustomView:btn];
    //    //使左侧按钮往左移动
    //    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
    //                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
    //                                       target:nil action:nil];
    //    negativeSpacer.width = -10;
    //    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, itemleftback, nil];
    [btn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 11)
    {
        BarButtonView *barBtnView = [[BarButtonView alloc] initWithFrame:btn.frame];
        [barBtnView setPosition:BarButtonViewPositionLeft];
        [barBtnView addSubview:btn];
        [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:barBtnView]];
    }
    else
    {
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
        [space setWidth:-10];
        [self.navigationItem setLeftBarButtonItems:@[space,[[UIBarButtonItem alloc] initWithCustomView:btn]]];
    }
}

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
